package com.wust.smartcity.generatorcode.task;

import com.wust.smartcity.generatorcode.task.base.AbstractTask;
import com.wust.smartcity.generatorcode.utils.ConfigUtil;
import com.wust.smartcity.generatorcode.utils.FileUtil;
import com.wust.smartcity.generatorcode.utils.FreemarkerConfigUtils;
import com.wust.smartcity.generatorcode.utils.StringUtil;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：wust
 * @date：2019-12-26
 */
public class ControllerTask extends AbstractTask {

    public ControllerTask(String childEntityPackageName,String className) {
        super(childEntityPackageName,className);
    }

    @Override
    public void run() throws IOException, TemplateException {
        String entityPackageName = ConfigUtil.getConfiguration().getPath().getEntityPackageName();
        if(childEntityPackageName != null && childEntityPackageName.replaceAll(" ","").length() > 0){
            String childEntityPackageNameTemp = childEntityPackageName;
            entityPackageName = entityPackageName + childEntityPackageNameTemp.replaceAll("_",".");
        }


        int j = 0;
        for (int i = 0; i < className.length(); i++) {
            if (i != 0 && Character.isUpperCase(className.charAt(i))) {
                j = i;
                break;
            }
        }
        String postfixName = className.substring(j);


        Map<String, String> controllerData = new HashMap<>();
        controllerData.put("BasePackageName", ConfigUtil.getConfiguration().getPackageName());
        controllerData.put("ControllerPackageName", ConfigUtil.getConfiguration().getPath().getController());
        if (StringUtil.isBlank(ConfigUtil.getConfiguration().getPath().getInterf())) {
            controllerData.put("ServicePackageName", ConfigUtil.getConfiguration().getPath().getService());
        } else {
            controllerData.put("ServicePackageName", ConfigUtil.getConfiguration().getPath().getInterf());
        }
        controllerData.put("EntityPackageName", entityPackageName);
        controllerData.put("Author", ConfigUtil.getConfiguration().getAuthor());
        controllerData.put("DateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        controllerData.put("ClassName", postfixName);
        controllerData.put("EntityName", className);
        controllerData.put("EntityNameForCamelCase", StringUtil.firstToLowerCase(className));
        String filePath = FileUtil.getSourcePath() + StringUtil.package2Path(ConfigUtil.getConfiguration().getPackageName()) + StringUtil.package2Path(ConfigUtil.getConfiguration().getPath().getController());
        String fileName = postfixName + "Controller.java";
        controllerData.put("FileName", fileName);
        // 生成Controller文件
        FileUtil.generateFile(FreemarkerConfigUtils.TYPE_CONTROLLER, controllerData, filePath + fileName);
    }
}
